<?php
class Search_Lucene {
    private $Lucene;
    const MAX_HITS = 50;
    const ENCODING = 'UTF-8';
    private $Path;
    const PREFIX_LENGTH = 4; /* lt 4 Performance warn */

    public function __construct() {
        $this->Path = Zend_Registry::get ( 'config' )->lucene->index;

        Zend_Search_Lucene::setResultSetLimit ( self::MAX_HITS );
        Zend_Search_Lucene_Analysis_Analyzer::setDefault ( new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive () );
        Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength ( self::PREFIX_LENGTH );
        Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding ( self::ENCODING );
        
        
        try {

            $this->Lucene = new Zend_Search_Lucene ( $this->Path, false );

        } catch ( Zend_Search_Lucene_Exception $e ) {
            $this->Lucene = new Zend_Search_Lucene ( $this->Path, true );
            $this->Lucene->setMaxMergeDocs ( 20 );
        }
    }

    public function ClearAll($value) {
        return mb_strtolower ( self::ClearText ( $this->StripTags ( $value ) ) );
    }

    public function ClearText($value) {
        $value = str_replace ( '\'', ' ', $value );
        $value = str_replace ( '"', ' ', $value );
        $value = str_replace ( ',', ' ', $value );
        $value = str_replace ( '.', ' ', $value );
        $value = str_replace ( '\\', ' ', $value );
        return $value;
    }

    public function optimize() {
        $this->Lucene->optimize ();
    }

    public function getInfo() {
        return array ('Index' => $this->Lucene->count (), 'Documents' => $this->Lucene->numDocs () );
    }

    public function search($queryString) {
        $query  = Zend_Search_Lucene_Search_QueryParser::parse ( $queryString );
        $result = $this->Lucene->find ( $query );
        $Array  = array ();

        foreach ( $result as $hit ) {
            $Array [] = array ('title' => $hit->title, 'teaser' => $hit->teaser, 'route' => $hit->route, 'type' => $hit->type, 'tag' => $hit->tag);
          }
          
        return $Array;
    }

    public function addDocument($title, $text, $teaser, $route, $type = '', $tag = '', $image = '')
    {
        $document = new Zend_Search_Lucene_Document ();

        $document->addField(Zend_Search_Lucene_Field::Text ('title', $title, 'utf-8'));
        $document->addField(Zend_Search_Lucene_Field::unStored('text', $text, 'utf-8'));
        $document->addField(Zend_Search_Lucene_Field::UnIndexed('teaser', $teaser, 'utf-8'));
        $document->addField(Zend_Search_Lucene_Field::UnIndexed('route', $route, 'utf-8'));
        $document->addField(Zend_Search_Lucene_Field::UnIndexed('type', $type, 'utf-8'));
        $document->addField(Zend_Search_Lucene_Field::UnIndexed('tag', $tag, 'utf-8'));
        $document->addField(Zend_Search_Lucene_Field::UnIndexed('image', $image, 'utf-8'));

        $this->Lucene->addDocument ( $document );
        $this->Lucene->commit ();
    }


    public function deleteByQuery($queryString) {
        $query = Zend_Search_Lucene_Search_QueryParser::parse ( $queryString );
        $result = $this->Lucene->find ( $query );
        $Array = array ();
        
        foreach ( $result as $hit ) {
            $this->Lucene->delete ( $hit->id );
        }

        return $Array;
    }

    function clearIndex() {
        for($count = 0; $count < $this->Lucene->maxDoc (); $count ++) {
            if ($this->Lucene->isDeleted ( $count )) {
            } else {
                $this->Lucene->delete ( $count );
            }
        }
        
        
        $this->Lucene->commit ();
    }

    function StripTags($value) {
        $Filter = new Zend_Filter_StripTags ();
        return $Filter->filter ( $value );
    }

}

